/**
 * Created by Administrator on 2015/3/18.
 */
var express = require('express');
var router = express.Router();
var wechat = require('wechat');
var fs = require('fs');

var wechatConfig = {
    token: 'huangshan_laili_200x',
    appid: 'wxf741aa74067383df',
    encodingAESKey: 'vevbDBsvkQEWWxmaNB8PLmCcuXtTBd37qTlJWwwzGH8'
};

router.use('/public', express.static('public'));

router.use('/wechat', wechat(wechatConfig, function(req, res){
    var configInfo = JSON.parse(fs.readFileSync(__dirname.replace('\lib', '')+'/config.json').toString());
    var message = req.weixin;
    switch (message.MsgType){
        case 'event':
            switch(message.Event){
                case 'subscribe':
                    var replyStr = configInfo.welcomeMessage;
                    break;
                case 'CLICK':
                    switch(message.EventKey){
                        case 'activity':
                            replyStr = configInfo.activityMessage;
                            break;
                        case 'record':
                            replyStr = configInfo.recordMessage;
                            break;
                        case 'course':
                            replyStr = configInfo.courseMessage;
                            break;
                        case 'classes':
                            replyStr = configInfo.classesMessage;
                            break;
                        case 'apply':
                            replyStr = configInfo.applyMessage;
                            break;
                        case 'about':
                            replyStr = configInfo.aboutMessage;
                            break;
                        case 'cooperation':
                            replyStr = configInfo.cooperationMessage;
                            break;
                    }
                    break;
            }
            res.reply(replyStr);
            break;
    }
}));

router.use('/', function (req, res) {
    res.send('Welcome to Natural-Li');
});

module.exports = router;